<?php

namespace App\Models;

use Validator;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Admin extends Model
{
    use SoftDeletes;
    protected $fillable = ['username', 'password', 'nickname', 'email'];
    protected $dateFormat = 'U';
    protected $dates = ['created_at', 'updated_at', 'deleted_at'];

    //登录处理
    public function login($data)
    {
        $rule = [
            'username' => 'bail|required',
            'password' => 'required'
        ];

        $msg = [
            'username.required' => '用户名不能为空',
            'password.required' => '密码不能为空'
        ];
        $validator = Validator::make($data, $rule, $msg);
        if ($validator->fails()) {
            return $validator->errors()->first();
        }
        $result = $this->where($data)->first();
        if ($result) {
            $sessionData = [
                'id' => $result['id'],
                'nickname' => $result['nickname'],
                'is_super' => $result['is_super']
            ];
            session(['admin' => $sessionData]);
            if ($result['status'] != 1) {
                return '账号被禁用';
            }
            return 1;
        }else {
            return '用户名或者密码错误';
        }
    }

    //注册
    public function register($data)
    {
        $rule = [
            'username' => 'bail|required|unique:admins',
            'password' => 'required',
            'conpass' => 'required|same:password',
            'nickname' => 'required',
            'email' => 'required|email|unique:admins'
        ];

        $msg = [
            'username.required' => '用户名必须填写',
            'username.unique' => '用户名已存在',
            'password.required' => '密码必须填写',
            'conpass.required' => '确认密码必须填写',
            'conpass.same' => '俩次输入密码不一致',
            'nickname.required' => '昵称必须填写',
            'email.required' => '邮箱必须填写',
            'email.email' => '邮箱格式不正确',
            'email.unique' => '此邮箱已经注册过'
        ];

        $validator = Validator::make($data, $rule, $msg);
        if ($validator->fails()) {
            return $validator->errors()->first();
        }
        $result = $this->create($data);
        if($result) {
            return 1;
        }else {
            return '注册失败';
        }
    }

    //发送验证码
    public function getCode($data)
    {
        $rule = [
            'email' => 'bail|required|email'
        ];

        $msg = [
            'email.required' => '邮箱不能为空',
            'email.email' => '邮箱格式不正确'
        ];

        $validator = Validator::make($data, $rule, $msg);
        if ($validator->fails()) {
            return $validator->errors()->first();
        }
        $result = $this->where('email', $data['email'])->first();
        if ($result) {
            $code = mt_rand(1000, 9999);
            session(['code' => $code]);
            $content = '您的验证码是' . $code;
            $sendResult = mailto($data['email'], '获取验证码--梦中程序员', $content);
            if ($sendResult) {
                return 1;
            }else {
                return '发送验证码失败';
            }
        }else {
            return '该邮箱尚未注册账号';
        }
    }

    public function reset($data)
    {
        $rule = [
            'code' => 'required'
        ];

        $msg = [
            'code.required' => '验证码不能为空'
        ];

        $validator = Validator::make($data, $rule, $msg);
        if ($validator->fails()) {
            return $validator->errors()->first();
        }
        if ($data['code'] != session('code')) {
            return '验证码不正确';
        }
        $password = mt_rand(10000, 99999);
        $adminInfo = $this->where('email', $data['email'])->first();
        $adminInfo->password = $password;
        $result = $adminInfo->save();
        if ($result) {
            $content = '您的用户名是' . $adminInfo['username'] . '<br>密码是：' . $password;
            $sendResult = mailto($data['email'], '恭喜您，密码重置成功', $content);
            if ($sendResult) {
                return 1;
            }else {
                return '密码重置失败';
            }
        }else {
            return '密码重置失败';
        }
    }

    //管理员添加
    public function add($data)
    {
        $rule = [
            'username' => 'bail|required|unique:admins',
            'password' => 'required',
            'nickname' => 'required',
            'email' => 'required|email|unique:admins'
        ];

        $msg = [
            'username.required' => '账号不能为空',
            'username.unique' => '账号已存在',
            'password.required' => '密码不能为空',
            'nickname.required' => '昵称不能为空',
            'email.required' => '邮箱不能为空',
            'email.email' => '邮箱格式不正确',
            'email.unique' => '此邮箱已注册过'
        ];

        $validator = Validator::make($data, $rule, $msg);
        if ($validator->fails()) {
            return $validator->errors()->first();
        }
        $result = $this->create($data);
        if ($result) {
            return 1;
        }else {
            return '管理员提交失败';
        }
    }

    //编辑
    public function edit($data)
    {
        $rule = [
            'oldpass' => 'bail|required',
            'password' => 'required',
            'nickname' => 'required'
        ];

        $msg = [
            'oldpass.required' => '原密码不能为空',
            'password.required' => '新密码不能为空',
            'nickname.required' => '昵称不能为空'
        ];

        $validator = Validator::make($data, $rule, $msg);
        if ($validator->fails()) {
            return $validator->errors()->first();
        }
        $adminInfo = $this->find($data['id']);
        if ($data['oldpass'] != $adminInfo->password) {
            return '原密码不正确';
        }
        $adminInfo->password = $data['password'];
        $adminInfo->nickname = $data['nickname'];
        $result = $adminInfo->save();
        if ($result) {
            return 1;
        }else {
            return '修改失败';
        }
    }
}
